package dao

import (
	"gorm.io/gorm"
	"xlq-demo/app/model"
	"xlq-demo/library/db"
)

type BusinessDao struct{}

func NewBusinessDao() *BusinessDao {
	return new(BusinessDao)
}

func (businessDao *BusinessDao) Create(business *model.Business) error {
	return db.GetMaster().Create(business).Error
}

func (businessDao *BusinessDao) Delete(id interface{}) error {
	return db.GetMaster().Where("id=?", id).Delete(new(model.Business)).Error
}

func (businessDao *BusinessDao) Update(id string, dataMap map[string]interface{}) error {
	return db.GetMaster().Model(new(model.Business)).Where("id=?", id).Updates(dataMap).Error
}

func (businessDao *BusinessDao) Find(id interface{}) (*model.Business, error) {
	business := new(model.Business)
	err := db.GetSlave().Where("id=?", id).Take(business).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return business, err
}

func (businessDao *BusinessDao) Get(ids []interface{}) ([]model.Business, error) {
	var businesss []model.Business
	err := db.GetSlave().Find(&businesss, ids).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return businesss, err
}

func (businessDao *BusinessDao) All() ([]model.Business, error) {
	var businesss []model.Business
	err := db.GetSlave().Find(&businesss).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return businesss, err
}

func (businessDao *BusinessDao) FindByWhere(wheres []string, params []interface{}, orders string) (*model.Business, error) {
	whereLen := len(wheres)
	business := new(model.Business)
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Take(&business).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return business, err
}

func (businessDao *BusinessDao) GetByWhere(wheres []string, params []interface{}, orders string) ([]model.Business, error) {
	whereLen := len(wheres)
	var businesss []model.Business
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Find(&businesss).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return businesss, err
}

func (businessDao *BusinessDao) CountByWhere(wheres []string, params []interface{}) (int64, error) {
	whereLen := len(wheres)
	business := new(model.Business)
	db := db.GetSlave().Model(business)
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	var total int64
	err := db.Count(&total).Error
	return total, err
}

func (businessDao *BusinessDao) ListByWhere(wheres []string, params []interface{}, orders string, offset, limit int) ([]model.Business, error) {
	var businesss []model.Business
	db := db.GetSlave()
	for i := 0; i < len(wheres); i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Offset(offset).Limit(limit).Find(&businesss).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return businesss, err
}

func (businessDao *BusinessDao) Raw(sql string, params []interface{}, out interface{}) error {
	err := db.GetSlave().Raw(sql, params...).Scan(out).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return err
}

func (businessDao *BusinessDao) Exec(sql string, params []interface{}) error {
	return db.GetSlave().Exec(sql, params).Error
}
